package com.tencent.basic.controller;

import com.tencent.basic.dto.SmsCodeDto;
import com.tencent.basic.exception.BusinessException;
import com.tencent.basic.service.IVerifyCodeService;
import com.tencent.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 接口 验证码
 * 手机验证码 和 图形验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码
     * @param key
     * @return
     */
    @RequestMapping("/image/{key}")
    @GetMapping
    public AjaxResult imageCode(@PathVariable("key") String key) {
        try {
            // 获取图形验证码的base64字符串
            String base64Str = verifyCodeService.getImageCode(key);
            // 通过AjaxResult设置resultObj返回给前端
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取图形验证码失败");
        }
    }

    /**
     * 发送手机验证码
     * @param smsCodeDto
     * @return
     */
    @RequestMapping("/smsCode")
    @PostMapping
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto) {
        try {
            // 获取图形验证码的base64字符串
            verifyCodeService.sendSmsCode(smsCodeDto);
            // 通过AjaxResult返回成功
            return AjaxResult.me();
        } catch (BusinessException b) {
            b.printStackTrace();
            // b.getMessage() 在前端还拼接了 发送失败
            return AjaxResult.me().setSuccess(false).setMessage(b.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙咯");
        }
    }

    /**
     * 发送手机验证码
     * @param smsCodeDto
     * @return
     */
    @RequestMapping("/binderSmsCode")
    @PostMapping
    public AjaxResult binderSmsCode(@RequestBody SmsCodeDto smsCodeDto) {
        try {
            // 获取图形验证码的base64字符串
            verifyCodeService.binderSmsCode(smsCodeDto);
            // 通过AjaxResult返回成功
            return AjaxResult.me();
        } catch (BusinessException b) {
            b.printStackTrace();
            // b.getMessage() 在前端还拼接了 发送失败
            return AjaxResult.me().setSuccess(false).setMessage(b.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙咯");
        }
    }

    /**
     * 检验邮箱注册验证码
     * @param smsCodeDto
     * @return
     */
    @RequestMapping("/emailCode")
    @PostMapping
    public AjaxResult emailCode(@RequestBody SmsCodeDto smsCodeDto) {
        try {
            // 获取图形验证码的base64字符串
            verifyCodeService.checkEmailCode(smsCodeDto);
            // 通过AjaxResult返回成功
            return AjaxResult.me();
        } catch (BusinessException b) {
            b.printStackTrace();
            // b.getMessage()
            return AjaxResult.me().setSuccess(false).setMessage(b.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙咯");
        }
    }

}
